﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.BillPlugIn.BPW
{
    [Description("BPW:票面字段规则开发需求-专票"), HotUpdate]
    public class SaleSicInfore : AbstractBillPlugIn
    {
        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);
            List<DynamicObject> list = new List<DynamicObject>();
            var obj = this.Model.DataObject;//SALESICENTRY
            DynamicObjectCollection rows = obj["SALESICENTRY"] as DynamicObjectCollection;
            for (int i = 0; i < rows.Count; i++)
            {
                //F_QHAT_Base_dvn 源出库单单据类型
                //F_BPW_KPNAME 开票名称 
                //F_BPW_KPModle  开票规格型号
                //F_BPW_KPQTY 开票数量
                //F_BPW_KPUnitID 开票单位
                //F_BPW_KPPrice 开票单价
                var mat = rows[i]["MATERIALID"] as DynamicObject;
                //string kpmc = mat["F_BPW_OUTNAME"].ToString();//物料上的出口报关名称
                string th = mat["F_BPW_Dwgnumber"].ToString();//图号
                //string kpdw = mat["F_BPW_OUTUnitID"].ToString();//报关单位
                var wlfz = mat["MaterialGroup"] as DynamicObject;//物料分组
                string wlfzName = mat["MaterialGroup"] == null ? "" : wlfz["name"].ToString();
                string wlName = mat["name"].ToString();//物料名称
                string wlNumber = mat["number"].ToString();//物料编码
                var rhzdw = mat["F_BPW_RHZUnitID"] as DynamicObject;//润滑脂单位
                string rhzdwNumber = mat["F_BPW_RHZUnitID"] == null ? "" : rhzdw["number"].ToString();
                DynamicObjectCollection matBase = mat["MaterialBase"] as DynamicObjectCollection;
                var chlb = matBase[0]["CategoryID"] as DynamicObject;
                //var chlb = mat["FCategoryID"] as DynamicObject;//存货类别
                string chlbNumber = chlb == null ? "" : chlb["name"].ToString();
                string czcs = mat["F_BPW_CZparameter"].ToString();//车轴参数
                var jbdw = matBase[0]["BaseUnitId"] as DynamicObject;
                //var jbdw = mat["FBaseUnitId"] as DynamicObject;//基本单位
                string baseUn = jbdw == null ? "" : jbdw["number"].ToString();

                decimal jjsl = Convert.ToDecimal(rows[i]["PRICEQTY"]);//计价数量
                decimal jshj = Convert.ToDecimal(rows[i]["ALLAMOUNTFOR"]);//价税合计
                //var dvn = rows[i]["F_QHAT_Base_dvn"] as DynamicObject;//源出库单单据类型
                //list.Add(dvn);
                //string dvname = dvn["name"].ToString();
                //if (dvname == "出口应收")
                //{
                //    this.Model.SetValue("F_BPW_KPNAME", kpmc, i);
                //    this.Model.SetValue("F_BPW_KPModle", th, i);
                //    this.Model.SetValue("F_BPW_KPQTY", jjsl, i);
                //    this.Model.SetValue("F_BPW_KPUnitID", kpdw, i);
                //    this.Model.SetValue("F_BPW_KPPrice", jshj / jjsl, i);
                //}
                //else 
                if (wlfzName == "润滑脂")
                {
                    DynamicObjectCollection rhzRows = GetRhzRows(this.Context, wlNumber);
                    decimal rhzQty = 1;
                    if (rhzRows != null && rhzRows.Count() > 0)
                    {
                        decimal qty1 = Convert.ToDecimal(rhzRows[0]["F_XRIS_QTY1"]);
                        decimal qty2 = Convert.ToDecimal(rhzRows[0]["F_XRIS_QTY2"]);
                        decimal qty3 = Convert.ToDecimal(rhzRows[0]["F_XRIS_QTY3"]);
                        decimal qty4 = Convert.ToDecimal(rhzRows[0]["F_XRIS_QTY4"]);
                        rhzQty = jjsl * qty1 * qty2 * qty4 / qty3;
                        this.Model.SetValue("F_BPW_KPNAME", wlName, i);
                        this.Model.SetValue("F_BPW_KPModle", wlNumber, i);
                        this.Model.SetValue("F_BPW_KPQTY", rhzQty, i);
                        this.Model.SetItemValueByNumber("F_BPW_KPUnitID", rhzdwNumber, i);
                        this.Model.SetValue("F_BPW_KPPrice", jshj / rhzQty, i);
                    }
                    //else
                    //{
                    //    this.Model.SetValue("F_BPW_KPNAME", wlName, i);
                    //    this.Model.SetValue("F_BPW_KPModle", wlNumber, i);
                    //    this.Model.SetValue("F_BPW_KPQTY", rhzQty, i);
                    //    this.Model.SetItemValueByNumber("F_BPW_KPUnitID", rhzdwNumber, i);
                    //    this.Model.SetValue("F_BPW_KPPrice", jshj / rhzQty, i);
                    //}
                }
                else if (chlbNumber == "成品车轴")
                {
                    string cbName = "BPW" + wlfzName + czcs;
                    this.Model.SetValue("F_BPW_KPNAME", cbName, i);
                    this.Model.SetValue("F_BPW_KPModle", wlName, i);
                    this.Model.SetValue("F_BPW_KPQTY", jjsl, i);
                    this.Model.SetItemValueByNumber("F_BPW_KPUnitID", baseUn, i);
                    this.Model.SetValue("F_BPW_KPPrice", jshj / jjsl, i);
                }
                else
                {
                    this.Model.SetValue("F_BPW_KPNAME", wlName, i);
                    this.Model.SetValue("F_BPW_KPModle", th, i);
                    this.Model.SetValue("F_BPW_KPQTY", jjsl, i);
                    this.Model.SetItemValueByNumber("F_BPW_KPUnitID", baseUn, i);
                    this.Model.SetValue("F_BPW_KPPrice", jshj / jjsl, i);
                }
            }
            //if (list.Distinct().Count() > 1)
            //{
            //    this.View.ShowErrMessage("不允许有多个源出库单单据类型！");
            //    e.Cancel = true;
            //}
        }

        private DynamicObjectCollection GetRhzRows(Context context, string wlNumber)
        {
            string sql = string.Format(@"/*dialect*/ 
            SELECT T0.F_XRIS_QTY1, 
                   T0.F_XRIS_QTY2, 
                   T0.F_XRIS_QTY3, 
                   T0.F_XRIS_QTY4
            FROM XRIS_t_Cust100006 T0
                 JOIN T_BD_MATERIAL T1 ON T1.FMATERIALID = T0.F_XRIS_MAT
            WHERE T1.FNUMBER = '{0}' ", wlNumber);
            DynamicObjectCollection doc = DBUtils.ExecuteDynamicObject(context, sql);
            return doc;
        }
    }
}
